Example Workflow
To use the fourT package the user must first read the patient file
using the ‘read_dexcom’ function.
df_dex1 <- read_dexcom("~/Desktop/R Workspace/4T/external files/4T.999_Clarity_Export_00999_Doe_John_2024-07-05_091012.csv")
This cleans entries which may have the date-time incorrectly inputted
(i.e. NA values) and reformats the data. The ‘read_dexcom’ function can
also optionally remove PHI and randomize the year to deidentify the
data; this is controlled by the ‘deid’ parameter.
| 2021-05-06 15:59:58 |
288 |
091012 |
NA |
| 2021-05-06 16:04:58 |
281 |
091012 |
NA |
| 2021-05-06 16:09:58 |
274 |
091012 |
NA |
| 2021-05-06 16:19:58 |
273 |
091012 |
NA |
| 2021-05-06 16:24:58 |
278 |
091012 |
NA |
| 2021-05-06 16:29:58 |
283 |
091012 |
NA |
| 2021-05-06 16:34:58 |
283 |
091012 |
NA |
| 2021-05-06 16:44:58 |
277 |
091012 |
NA |
| 2021-05-06 16:49:58 |
275 |
091012 |
NA |
| 2021-05-06 16:54:58 |
273 |
091012 |
NA |
| 2021-05-06 16:59:58 |
270 |
091012 |
NA |
| 2021-05-06 17:04:58 |
269 |
091012 |
NA |
| 2021-05-06 17:09:58 |
265 |
091012 |
NA |
| 2021-05-06 17:54:58 |
289 |
091012 |
NA |
| 2021-05-06 17:59:59 |
278 |
091012 |
NA |
| 2021-05-06 18:04:58 |
262 |
091012 |
NA |
| 2021-05-06 18:09:58 |
258 |
091012 |
NA |
| 2021-05-06 18:14:58 |
258 |
091012 |
NA |
| 2021-05-06 18:19:58 |
253 |
091012 |
NA |
| 2021-05-06 18:24:59 |
249 |
091012 |
NA |
The default setting of ‘read_dexcom’ is with ‘deid = FALSE’,
i.e. without deidentification. This reflects usage inside of the QSU
where deidenitfying the data may not be an issue. When working with
outside collaborators the deidentification can be switched on by setting
the ‘deid’ parameter to TRUE.
df_dex2 <- read_dexcom("~/Desktop/R Workspace/4T/external files/4T.999_Clarity_Export_00999_Doe_John_2024-07-05_091012.csv", deid = T)
| 2077-05-06 15:59:58 |
288 |
091012 |
NA |
| 2077-05-06 16:04:58 |
281 |
091012 |
NA |
| 2077-05-06 16:09:58 |
274 |
091012 |
NA |
| 2077-05-06 16:19:58 |
273 |
091012 |
NA |
| 2077-05-06 16:24:58 |
278 |
091012 |
NA |
| 2077-05-06 16:29:58 |
283 |
091012 |
NA |
| 2077-05-06 16:34:58 |
283 |
091012 |
NA |
| 2077-05-06 16:44:58 |
277 |
091012 |
NA |
| 2077-05-06 16:49:58 |
275 |
091012 |
NA |
| 2077-05-06 16:54:58 |
273 |
091012 |
NA |
After reading the patient file, any of the computation or
visualization functions can be applied independently. For example, the
user could compute wear time, AGP thermoemters and GMI every 14 days,
with a 90 day lookback window.
compute_agp(df_dex1, freq = 14)
#> [1] "Time interval 1 does not have enough data, values set to NA"
#> [1] "Time interval 2 does not have enough data, values set to NA"
#> # A tibble: 28 × 6
#> n tbr2 tbr tar tar2 tir
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 87 NA NA NA NA NA
#> 2 2646 NA NA NA NA NA
#> 3 6601 0.0103 0.0721 0.200 0.0530 0.728
#> 4 8194 0.0101 0.0832 0.182 0.0428 0.734
#> 5 10366 0.0346 0.108 0.236 0.0820 0.656
#> 6 10366 0.0346 0.108 0.236 0.0820 0.656
#> 7 12063 0.0384 0.112 0.263 0.0958 0.625
#> 8 12343 0.0429 0.120 0.319 0.113 0.561
#> 9 13738 0.0451 0.128 0.357 0.143 0.515
#> 10 14389 0.0491 0.124 0.415 0.165 0.461
#> # ℹ 18 more rows
compute_gmi(df_dex1, freq = 14)
#> [1] "Time interval 1 does not have enough data, values set to NA"
#> [1] "Time interval 2 does not have enough data, values set to NA"
#> bg_mean bg_total gmi n
#> 1 NA NA NA NA
#> 2 NA NA NA NA
#> 3 135.3048 893147 6.546491 6601
#> 4 132.1153 1082553 6.470199 8194
#> 5 140.0645 1451909 6.660344 10366
#> 6 140.0645 1451909 6.660344 10366
#> 7 144.4780 1742838 6.765914 12063
#> 8 152.8727 1886908 6.966715 12343
#> 9 158.8904 2182836 7.110658 13738
#> 10 167.5433 2410781 7.317636 14389
#> 11 159.8004 2866021 7.132427 17935
#> 12 150.1126 2739555 6.900693 18250
#> 13 144.7340 3218885 6.772038 22240
#> 14 144.9558 3627809 6.777343 25027
#> 15 139.6998 3524905 6.651619 25232
#> 16 136.0809 3427605 6.565054 25188
#> 17 135.9112 3427272 6.560995 25217
#> 18 134.0086 3373800 6.515485 25176
#> 19 136.8940 3484089 6.584504 25451
#> 20 136.0141 3448501 6.563457 25354
#> 21 137.2981 3490256 6.594171 25421
#> 22 137.2221 3491478 6.592352 25444
#> 23 138.8988 3531364 6.632460 25424
#> 24 143.0435 3654189 6.731600 25546
#> 25 144.9528 3684120 6.777271 25416
#> 26 145.0307 3697847 6.779134 25497
#> 27 144.1629 3674568 6.758376 25489
#> 28 146.0801 3731763 6.804237 25546
compute_wear_time(df_dex1)
#> # A tibble: 383 × 5
#> date day_number n wear_prop total_wear_prop
#> <date> <dbl> <int> <dbl> <chr>
#> 1 2021-05-07 0 287 0.997 <NA>
#> 2 2021-05-08 1 288 1 <NA>
#> 3 2021-05-09 2 288 1 <NA>
#> 4 2021-05-10 3 288 1 <NA>
#> 5 2021-05-11 4 288 1 <NA>
#> 6 2021-05-12 5 288 1 <NA>
#> 7 2021-05-13 6 288 1 <NA>
#> 8 2021-05-14 7 288 1 <NA>
#> 9 2021-05-15 8 100 0.347 <NA>
#> 10 2021-05-16 9 NA NA <NA>
#> # ℹ 373 more rows
There are multiple different options for breaking up the time window
for each function.
compute_agp(df_dex1, freq = 14)
#> [1] "Time interval 1 does not have enough data, values set to NA"
#> [1] "Time interval 2 does not have enough data, values set to NA"
#> # A tibble: 28 × 6
#> n tbr2 tbr tar tar2 tir
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 87 NA NA NA NA NA
#> 2 2646 NA NA NA NA NA
#> 3 6601 0.0103 0.0721 0.200 0.0530 0.728
#> 4 8194 0.0101 0.0832 0.182 0.0428 0.734
#> 5 10366 0.0346 0.108 0.236 0.0820 0.656
#> 6 10366 0.0346 0.108 0.236 0.0820 0.656
#> 7 12063 0.0384 0.112 0.263 0.0958 0.625
#> 8 12343 0.0429 0.120 0.319 0.113 0.561
#> 9 13738 0.0451 0.128 0.357 0.143 0.515
#> 10 14389 0.0491 0.124 0.415 0.165 0.461
#> # ℹ 18 more rows
compute_gmi(df_dex1, breaks = c(0, 1, 3, 6, 7.5, 9, 12))
#> # A tibble: 6 × 4
#> inter bg_mean bg_total gmi
#> <int> <dbl> <dbl> <dbl>
#> 1 1 135. 962505 6.53
#> 2 2 161. 914160 7.17
#> 3 3 145. 3651254 6.78
#> 4 4 138. 1743626 6.61
#> 5 5 134. 1736416 6.51
#> 6 6 145. 3719327 6.77
compute_wear_time(df_dex1, start = , end = )
#> # A tibble: 383 × 5
#> date day_number n wear_prop total_wear_prop
#> <date> <dbl> <int> <dbl> <chr>
#> 1 2021-05-07 0 287 0.997 <NA>
#> 2 2021-05-08 1 288 1 <NA>
#> 3 2021-05-09 2 288 1 <NA>
#> 4 2021-05-10 3 288 1 <NA>
#> 5 2021-05-11 4 288 1 <NA>
#> 6 2021-05-12 5 288 1 <NA>
#> 7 2021-05-13 6 288 1 <NA>
#> 8 2021-05-14 7 288 1 <NA>
#> 9 2021-05-15 8 100 0.347 <NA>
#> 10 2021-05-16 9 NA NA <NA>
#> # ℹ 373 more rows
If one would like to instead compute all of these metrics at the same
time, with the same time window partition they can simply call the
wrapping function ‘cgm_summaries()’.
cgm_summaries(df_dex1, breaks = c(0, 1, 3, 6, 7.5, 9, 12))
#> New names:
#> • `inter` -> `inter...1`
#> • `inter` -> `inter...8`
#> # A tibble: 6 × 11
#> inter...1 n tbr2 tbr tar tar2 tir inter...8 bg_mean bg_total
#> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl>
#> 1 1 7153 0.00951 0.0762 0.192 0.0489 0.731 1 135. 962505
#> 2 2 5663 0.0828 0.166 0.380 0.168 0.454 2 161. 914160
#> 3 3 25202 0.0314 0.120 0.272 0.0947 0.608 3 145. 3651254
#> 4 4 12635 0.0293 0.110 0.233 0.0624 0.657 4 138. 1743626
#> 5 5 12984 0.0281 0.0860 0.188 0.0323 0.727 5 134. 1736416
#> 6 6 25705 0.0149 0.0678 0.248 0.0642 0.684 6 145. 3719327
#> # ℹ 1 more variable: gmi <dbl>
This function can also be used to simultaneously create the AGP
thermometer stacked bar plot, by setting ‘plot = T’ (the above tabular
output is hidden when cgm_summaries() is called below).
cgm_summaries(df_dex1, breaks = c(0, 1, 3, 6, 7.5, 9, 12), plot = T)

A similar workflow works for cohorts. The user should first read the
cohort data using the ‘read_cohort_dexcom’ function.
df_cohort_dex <- read_cohort_dexcom("~/Desktop/R Workspace/4T/external files/cohort-ex")
| 2021-05-06 15:59:58 |
288 |
091012 |
NA |
| 2021-05-06 15:59:58 |
288 |
091399 |
NA |
| 2021-05-06 15:59:58 |
288 |
987654 |
NA |
| 2021-05-06 16:04:58 |
281 |
091012 |
NA |
| 2021-05-06 16:04:58 |
281 |
091399 |
NA |
| 2021-05-06 16:04:58 |
281 |
987654 |
NA |
| 2021-05-06 16:09:58 |
274 |
091012 |
NA |
| 2021-05-06 16:09:58 |
274 |
091399 |
NA |
| 2021-05-06 16:09:58 |
274 |
987654 |
NA |
| 2021-05-06 16:19:58 |
273 |
091012 |
NA |
| 2021-05-06 16:19:58 |
273 |
091399 |
NA |
| 2021-05-06 16:19:58 |
273 |
987654 |
NA |
| 2021-05-06 16:24:58 |
278 |
091012 |
NA |
| 2021-05-06 16:24:58 |
278 |
091399 |
NA |
| 2021-05-06 16:24:58 |
278 |
987654 |
NA |
| 2021-05-06 16:29:58 |
283 |
091012 |
NA |
| 2021-05-06 16:29:58 |
283 |
091399 |
NA |
| 2021-05-06 16:29:58 |
283 |
987654 |
NA |
| 2021-05-06 16:34:58 |
283 |
091012 |
NA |
| 2021-05-06 16:34:58 |
283 |
091399 |
NA |
‘read_cohort_dexcom’ may take more than just the filepath as input.
It also uses two paraemters ‘indiv’ and ‘ordered’. ‘indiv’ determines
whether patient files should be read into seperate data frames, or one
large data frame. ‘ordered’ is used only when ‘indiv’ = FALSE. If
‘ordered’ = TRUE then the combined data frame rows are rearranged in
chronological order. If ‘ordered’ = FALSE then the combined data frame
remains seperated by patient. When ‘indiv’ is FALSE,
‘read_cohort_dexcom’ returns a list of data frames.
After reading the cohort dexcom data, the user can use the cohort
cgm_summaries function ‘cohort_cgm_summaries’ to compute the same
summaries at the cohort level. ‘cohort_cgm_summaries’ takes the same
parameters as the patient ‘cgm_summaries’ functions, along with the
parameters added to the ‘read_cohort_dexcom’ function. The user can
again choose whether to plot the AGP thermometers.
cohort_cgm_summaries(df_cohort_dex, breaks = c(0, 1, 3, 6, 7.5, 9, 12), plot = T)
#> New names:
#> • `inter` -> `inter...1`
#> • `inter` -> `inter...8`
| 1 |
21459 |
0.0095065 |
0.0761918 |
0.1923668 |
0.0489305 |
0.7314414 |
1 |
134.5596 |
2887515 |
6.528666 |
| 2 |
16989 |
0.0828183 |
0.1663429 |
0.3796574 |
0.1675790 |
0.4539996 |
2 |
161.4268 |
2742480 |
7.171329 |
| 3 |
75606 |
0.0313864 |
0.1201492 |
0.2718435 |
0.0946750 |
0.6080073 |
3 |
144.8795 |
10953762 |
6.775518 |
| 4 |
37905 |
0.0292837 |
0.1096953 |
0.2332410 |
0.0623664 |
0.6570637 |
4 |
137.9997 |
5230878 |
6.610952 |
| 5 |
38952 |
0.0281115 |
0.0859519 |
0.1875385 |
0.0323475 |
0.7265096 |
5 |
133.7351 |
5209248 |
6.508943 |
| 6 |
77115 |
0.0148998 |
0.0677689 |
0.2478895 |
0.0641509 |
0.6843416 |
6 |
144.6927 |
11157981 |
6.771051 |
Computation Functions
There are four computation functions: ‘compute_gmi’, ‘compute_agp’,
‘compute_wear_time’, and ‘compute_avg_glucose’. Each computation
function operates without reference to another, except for
‘compute_gmi’, which calls ‘compute_avg_glucose’; the
‘compute_avg_glucose’ function can also be called independently by the
user.
Time control parameters: There are three primary ways in which to
time window for computation can be modified:
- ‘start’ and ‘end’ parameters
- ‘inter’ parameter
- ‘breaks’ parameter
The ‘start’ and ‘end’ parameters are straightforward, the user can
manually enter start and end dates for the individual patient. If no
start and end dates are inputted, the default values are … something …
.
The ‘inter’ parameter allows for the specification of regular
interval windows in which the metric should be computed. When using the
‘inter’ parameter, the user should be intentional with their choice of
‘cut_reference’ value. The intervals are computed based on the start
date or end date depending on the choice of ‘cut_reference’ value.
Example 1: start = “default”, end = “default”, inter = 14, breaks =
NULL, cut_reference = “end”
Use of ‘inter’ parameter with default ‘start
’and ’end’ points
The ‘breaks’ parameter allows metrics to be computed at different
time points, e.g. 1 month, 2 months, 6 months, 12 months.
Example 2: start = “default”, end = “default”, breaks = c(0, 1, 3, 6,
12)
Use of ‘breaks’ parameter with default ‘start
’and ’end’ points
Example 3:
df_dex <- read_dexcom("~/Desktop/R Workspace/4T/external files/4T.399_Clarity_Export_00123_Doe_John_2024-05-01_162849.csv")
fourT::compute_gmi(df_dex = df_dex, start = "default", end = "default", inter = 14, breaks = c(0, 1, 3, 6, 12))
#> Error in fourT::compute_gmi(df_dex = df_dex, start = "default", end = "default", : unused argument (inter = 14)
Both ‘inter’ and ‘breaks’ are specified, which is not allowed.